package com.kwedding.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.kwedding.service.SendMail;

@ComponentScan
@Component
public class AllException extends LoggerParent implements HandlerExceptionResolver {

	@Autowired
	private SendMail mailUtil;

	@Override
	public ModelAndView resolveException(HttpServletRequest paramHttpServletRequest,
			HttpServletResponse paramHttpServletResponse, Object paramObject, Exception paramException) {

		
		//mailUtil.sendException(paramHttpServletRequest, paramException);
		log.info("exception catch>>>:" + new Exception(paramException));
		FastJsonJsonView view = new FastJsonJsonView();
		Map<String, Object> attributes = new HashMap<String, Object>();
		if(paramException instanceof RequestErrorBackException) {
			RequestErrorBackException requestErrorBackException = (RequestErrorBackException)paramException;
			attributes.put("code", requestErrorBackException.getErrorCode());
			attributes.put("msg", requestErrorBackException.getMsg());
		}else {
			attributes.put("code", 1);
			attributes.put("msg", paramException);
		}
		
		// attributes.put("msg", paramException.getMessage());
		view.setAttributesMap(attributes);
		ModelAndView mv = new ModelAndView();
		mv.setView(view);
		return mv;
	}

}
